from Util.Vertex import Vertex


class Graph:

    def __init__(self):
        self.vertices = []
        self.edges = []
        self.vertex_index = {}
        self.start_time = 1970

    def get_neighbor(self, vertex):
        neighbors = []
        directions = []
        edges = []

        for edge in self.edges:
            if vertex in edge.get_vertices():
                neighbor = edge.get_other(vertex)
                direct = 2
                if edge.start_with(vertex):
                    direct = 1
                if neighbor not in neighbors:
                    neighbors.append(neighbor)
                    directions.append(direct)
                    edges.append([edge])
                    continue
                index = neighbors.index(neighbor)
                if direct != directions[index]:
                    directions[index] = 3
                edges[index].append(edge)

        return neighbors, directions, edges

    def add_edge(self, edge):
        self.edges.append(edge)
        for vertex in edge.get_vertices():
            if vertex not in self.vertex_index.keys():
                index = len(self.vertices)
                self.vertex_index[vertex] = index
                self.vertices.append(Vertex(vertex))
            vertex_index = self.vertex_index[vertex]
            vertex_instance = self.vertices[vertex_index]
            vertex_instance.add_contact(edge)

    def get_vertex(self, node):
        if node in self.vertex_index:
            index = self.vertex_index[node]
            vertex = self.vertices[index]
            return vertex
